﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;

namespace Eneta.Portal.Common.Helpers
{
    /// <summary>
    /// Class for image manipulation methods.
    /// </summary>
    public class ImageHelper
    {
        /// <summary>
        /// Resizes the image to given dimensions.
        /// </summary>
        /// <param name="width">The width of target image.</param>
        /// <param name="height">The height of target image.</param>
        /// <param name="fromStream">Stream with source image bytes.</param>
        /// <param name="toStream">Stream where target image bytes are written.</param>
        public static void ResizeImage(int width, int height, Stream fromStream, Stream toStream)
        {
            var image = Image.FromStream(fromStream);
            var x1 = image.Width;
            var y1 = image.Height;

            var x2 = width;
            var y2 = height;
            int x11, y11;

            if (x1 < width)
            {
                var pictureContents = new Bitmap(fromStream);
                pictureContents.Save(toStream, ImageFormat.Png);
                return;
            }
            else
            {
                var imageWidth = (double)x1;
                double widthRatio = width / imageWidth ;
                x11 = width;
                y11 = (int)(y1 * widthRatio);
            }

            var thumbnailBitmap = new Bitmap(x11, y11);

            var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

            var imageRectangle = new Rectangle(0, 0, x11, y11);
            thumbnailGraph.DrawImage(image, imageRectangle);

            //thumbnailBitmap.Save(toStream, image.RawFormat);
            thumbnailBitmap.Save(toStream, ImageFormat.Png);

            thumbnailGraph.Dispose();
            thumbnailBitmap.Dispose();
            image.Dispose();
        }

        /// <summary>
        /// Determines whether image in given stream is valid.
        /// </summary>
        /// <param name="imageFileStream">The stream with image bytes.</param>
        /// <returns>
        /// 	<c>true</c> if image is valid; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValidImage(Stream imageFileStream)
        {
            BitmapImage image;
            try
            {
                image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = imageFileStream;
                image.CacheOption = BitmapCacheOption.None;
                image.EndInit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
